package com.ssm.controller;

import com.ssm.entity.Menu;
import com.ssm.service.MenuServiceI;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/mc")
public class MenuController extends BaseController {

    @Resource(name = "menuServiceI")
    private MenuServiceI msi;

    @RequestMapping("/index")
    public String index(){
        return prefix + "/menu/index" + suffix;
    }

    @RequestMapping("/aoe")
    public String aoe(HttpServletRequest req){
        String id = req.getParameter("id");
        if(id!=null&&id!=""){
            Menu m = new Menu();
            m.setId(Integer.parseInt(id));
            m = msi.get(m);
            req.setAttribute("u",m);
        }
        return prefix + "/menu/aoe" + suffix;
    }

    @RequestMapping("/find")
    @ResponseBody
    public List<Menu> find(Menu menu){
        return msi.find(menu);
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(Menu menu){
        return msi.add(menu);
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(Menu menu){
        return msi.edit(menu);
    }


    @RequestMapping("/removeMenu/{id}")
    @ResponseBody
    public Map<String,Object> removeMenu(@PathVariable(value ="id") int id){
        Menu menu = new Menu();
        menu.setId(id);
        return msi.batchRemove(menu);
    }

    @RequestMapping("/getMenuByRoleId")
    @ResponseBody
    public String getMenuByRoleId(@Param(value = "roleId") int roleId){
        return msi.getMenuByRoleId(roleId);
    }
}
